package org.mspring.platform.api.rss.writers.defaults;

import java.util.Iterator;
import java.util.List;

import org.mspring.platform.api.rss.elements.Channel;
import org.mspring.platform.api.rss.elements.Item;
import org.mspring.platform.api.rss.elements.SyndicatedElement;
import org.mspring.platform.api.rss.writers.GenericRSSWriter;

/**
 * 
 * @author Gao Youbo
 * @since 2013年7月26日
 */
@SuppressWarnings({ "rawtypes" })
public class RSS300 extends GenericRSSWriter {
    public String getFeed(Channel paramChannel) {
        StringBuffer localStringBuffer = new StringBuffer();
        buildHead(localStringBuffer, paramChannel);
        addItems(localStringBuffer, paramChannel.getItems());
        return localStringBuffer.toString();
    }

    private void addItems(StringBuffer paramStringBuffer, List paramList) {
        Item localItem = null;
        if (paramList != null) {
            Iterator localIterator = paramList.iterator();
            while (localIterator.hasNext()) {
                localItem = (Item) localIterator.next();
                addItem(paramStringBuffer, localItem);
            }
        }
    }

    private void addItem(StringBuffer paramStringBuffer, Item paramItem) {
        addSyndicatedElement(paramStringBuffer, paramItem, new CustomWriter() {
            public void addData(StringBuffer paramStringBuffer, SyndicatedElement paramSyndicatedElement) {
            }
        });
    }

    private void buildHead(StringBuffer paramStringBuffer, Channel paramChannel) {
        addSyndicatedElement(paramStringBuffer, paramChannel, new CustomWriter() {
            public void addData(StringBuffer paramStringBuffer, SyndicatedElement paramSyndicatedElement) {
                paramStringBuffer.append(RSS300.this.buildLine("errorsto", null));
            }
        });
    }

    private void addSyndicatedElement(StringBuffer paramStringBuffer, SyndicatedElement paramSyndicatedElement, CustomWriter paramCustomWriter) {
        paramStringBuffer.append(buildLine("title", paramSyndicatedElement.getTitle()));
        paramStringBuffer.append(buildLine("description", paramSyndicatedElement.getDescription()));
        paramStringBuffer.append(buildLine("link", paramSyndicatedElement.getLink()));
        paramStringBuffer.append(buildLine("generator", "RSSLibJ http://enigmastation.com/rsslibj"));
        paramStringBuffer.append(buildLine("creator", paramSyndicatedElement.getDcCreator()));
        paramStringBuffer.append(buildLine("created", paramSyndicatedElement.getDcDate()));
        paramStringBuffer.append(buildLine("last-modified", null));
        paramStringBuffer.append(buildLine("language", paramSyndicatedElement.getLanguage()));
        paramStringBuffer.append(buildLine("rights", paramSyndicatedElement.getDcRights()));
        paramStringBuffer.append(buildLine("license", null));
        paramStringBuffer.append(buildLine("uri", paramSyndicatedElement.getLink()));
        paramStringBuffer.append(buildLine("subject", null));
        paramCustomWriter.addData(paramStringBuffer, paramSyndicatedElement);
        paramStringBuffer.append("\n");
    }

    private StringBuffer buildLine(String paramString, Object paramObject) {
        StringBuffer localStringBuffer = new StringBuffer();
        if (paramObject != null) {
            localStringBuffer.append(paramString);
            localStringBuffer.append(": ");
            localStringBuffer.append(GenericRSSWriter.stringValue(paramObject));
            localStringBuffer.append("\n");
        }
        return localStringBuffer;
    }
}